/*
 * Copyright (c) 2015. 速位中国
 */

package com.kit;
/*

                  写字楼里写字间，写字间里程序员；
                  程序人'猿'写程序，又拿程序换酒钱。
                  酒醒只在网上坐，酒醉还来网下眠；
                  酒醉酒醒日复日，网上网下年复年。
                  但愿老死电脑间，不愿鞠躬老板前；
                  奔驰宝马贵者趣，公交自行程序员。
                  别人笑我忒疯癫，我笑自己命太贱；
                  不见满街漂亮妹，哪个归得程序'猿'？
━━━━━━神兽出没━━━━━━
 ┏┓   ┏┓
┏┛┻━━━┛┻┓
┃       ┃
┃   ━   ┃
┃ ┳┛ ┗┳ ┃
┃       ┃
┃   ┻   ┃
┃       ┃
┗━┓   ┏━┛
  ┃   ┃                    神兽保佑, 永无BUG!
  ┃   ┃       Code is far away from bug with the animal protecting
  ┃   ┗━━━┓
  ┃       ┣┓
  ┃       ┏┛
  ┗┓┓┏━┳┓┏┛
   ┃┫┫ ┃┫┫
   ┗┻┛ ┗┻┛
 */

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by 石头哥哥 on 12/21/15.
 * <p/>
 * server-service
 * <p/>
 * com.suweia.intellibox.server_service.utils
 * <p/>
 * comments:
 */
public class NamedThreadFactory implements ThreadFactory {

    private int _prio;
    private String _name;
    private AtomicInteger _threadNumber = new AtomicInteger(1);
    private ThreadGroup _group;

    public String get_name() {
        return _name;
    }

    /**
     * @param name 线程池名
     */
    public NamedThreadFactory(String name) {
        _prio = Thread.NORM_PRIORITY;//线程默认优先级
        _name = name;
        _group = new ThreadGroup(_name);
    }


    /**
     * @param name 线程池名
     */
    public NamedThreadFactory(String name, int _prio) {
        _prio = _prio;
        _name = name;
        _group = new ThreadGroup(_name);
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(_group, r);
        t.setName(_name + "-" + "#-" + _threadNumber.getAndIncrement());
        t.setPriority(_prio);
        return t;
    }

    public ThreadGroup getGroup() {
        return _group;
    }
}
